package org.flashbrighton.as3bots.components
{
	import Box2D.Dynamics.b2Body;
	import org.flashbrighton.as3bots.models.BotModel;
	
	public class SteeringComponent implements IComponent
	{
		public var maxTorque:int = 100;
		
		private var _power:Number;
		
		
		public function SteeringComponent()
		{
			_power = 0.0;
		}
		
		
		public function update(botModel:BotModel):void
		{
			setPower(botModel.steeringPower);
			rotate(botModel.body);
			
			// Reset power in case it is not
			// set when this method is called
			_power = 0.0;
		}
		
		
		private function rotate(body:b2Body):void
		{
			body.ApplyTorque(_power * maxTorque);			
		}
		
		
		private function setPower(val:Number):void
		{
			// Limits
			if (val > 1) val = 1;
			else if (val < -1) val = -1;
			
			_power = val;
		}
		
	}
}